#region File Description
//-----------------------------------------------------------------------------
// MenuScreen.cs
//
// XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;



#endregion

namespace CreamX.Framework.GameState.Screens
{

    /// <summary>
    /// Base class for screens that contain a menu of options. The user can
    /// move up and down to select an entry, or cancel to back out of the screen.
    /// </summary>
    public abstract class MenuScreen : GameScreen
    {
        #region Fields

        List<MenuEntry> menuEntries = new List<MenuEntry>();
        public int selectedEntry = 0;
        string menuTitle;

        #endregion

        #region Properties
        private int _menuIndent=0;

        public int MenuIndent
        {
            get { return _menuIndent; }
            set { _menuIndent = value; }
        }


        /// <summary>
        /// Gets the list of menu entries, so derived classes can add
        /// or change the menu contents.
        /// </summary>
        protected IList<MenuEntry> MenuEntries
        {
            get { return menuEntries; }
        }


        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public MenuScreen(string menuTitle)
        {
            this.menuTitle = menuTitle;

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }


        #endregion

        #region Handle Input
        

        /// <summary>
        /// Responds to user input, changing the selected entry and accepting
        /// or cancelling the menu.
        /// </summary>
        public override void HandleInput(GameTime gameTime, InputState input)
        {
            // Move to the previous menu entry?
            if (input.MenuUp)
            {
                selectedEntry--;

                if (selectedEntry < 0)
                    selectedEntry = menuEntries.Count - 1;
            }

            // Move to the next menu entry?
            if (input.MenuDown)
            {
                selectedEntry++;

                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0;
            }

            // Accept or cancel the menu?
            if (input.MenuSelect)
            {
                OnSelectEntry(selectedEntry);
            }
            else if (input.MenuCancel)
            {
                OnCancel();
            }
        }


        /// <summary>
        /// Handler for when the user has chosen a menu entry.
        /// </summary>
        protected virtual void OnSelectEntry(int entryIndex)
        {
            menuEntries[selectedEntry].OnSelectEntry();
        }


        /// <summary>
        /// Handler for when the user has cancelled the menu.
        /// </summary>
        protected virtual void OnCancel()
        {
            ExitScreen();
        }


        /// <summary>
        /// Helper overload makes it easy to use OnCancel as a MenuEntry event handler.
        /// </summary>
        protected void OnCancel(object sender, EventArgs e)
        {
            OnCancel();
        }


        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the menu.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // Update each nested MenuEntry object.
            for (int i = 0; i < menuEntries.Count; i++)
            {
                bool isSelected = IsActive && (i == selectedEntry);

                menuEntries[i].Update(this, isSelected, gameTime);
            }
        }
        public Vector2 startPosition = new Vector2(100, 150);

        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            Vector2 position = startPosition;

            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            if (ScreenState == ScreenState.TransitionOn)
                position.X -= transitionOffset * 256;
            else
                position.X += transitionOffset * 512;

            spriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuEntry menuEntry = menuEntries[i];

                bool isSelected = IsActive && (i == selectedEntry);

                menuEntry.Draw(this, position, isSelected, gameTime);
                menuEntry.normalColor = normalColor;
                menuEntry.highLightColor = highLightColor;
                position.Y += menuEntry.GetHeight(this);
                position.X += MenuIndent;
            }

            // Draw the menu title.
            Vector2 titlePosition = new Vector2(533, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192, TransitionAlpha);
            float titleScale = 1.25f;

            titlePosition.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }
        public Color normalColor = Color.White;
        public Color highLightColor = Color.Yellow;
        
        #endregion
    }
    ///// <summary>
    ///// Base class for screens that contain a menu of options. The user can
    ///// move up and down to select an entry, or cancel to back out of the screen.
    ///// </summary>
    //public abstract class MenuScreen : GameScreen
    //{
    //    #region Fields

    //    List<string> menuEntries = new List<string>();
    //    protected int selectedEntry = 0;

    //    #endregion

    //    #region Properties


    //    /// <summary>
    //    /// Gets the list of menu entry strings, so derived classes can add
    //    /// or change the menu contents.
    //    /// </summary>
    //    protected IList<string> MenuEntries
    //    {
    //        get { return menuEntries; }
    //    }
    //    private Vector2 _position=new Vector2(100, 150);

    //    public Vector2 MenuPosition
    //    {
    //        get { return _position; }
    //        set { _position = value; }
    //    }

    //    #endregion

    //    #region Initialization


    //    /// <summary>
    //    /// Constructor.
    //    /// </summary>
    //    public MenuScreen()
    //    {
    //        TransitionOnTime = TimeSpan.FromSeconds(0.5);
    //        TransitionOffTime = TimeSpan.FromSeconds(0.5);
    //    }


    //    #endregion

    //    #region Handle Input


    //    /// <summary>
    //    /// Responds to user input, changing the selected entry and accepting
    //    /// or cancelling the menu.
    //    /// </summary>
    //    public override void HandleInput(GameTime gameTime, InputState input)
    //    {
    //        // Move to the previous menu entry?
    //        if (input.MenuUp)
    //        {
    //            selectedEntry--;

    //            if (selectedEntry < 0)
    //                selectedEntry = menuEntries.Count - 1;
    //        }

    //        // Move to the next menu entry?
    //        if (input.MenuDown)
    //        {
    //            selectedEntry++;

    //            if (selectedEntry >= menuEntries.Count)
    //                selectedEntry = 0;
    //        }

    //        // Accept or cancel the menu?
    //        if (input.MenuSelect)
    //        {
    //            OnSelectEntry(selectedEntry);
    //        }
    //        else if (input.MenuCancel)
    //        {
    //            OnCancel();
    //        }
    //    }


    //    /// <summary>
    //    /// Notifies derived classes that a menu entry has been chosen.
    //    /// </summary>
    //    protected abstract void OnSelectEntry(int entryIndex);


    //    /// <summary>
    //    /// Notifies derived classes that the menu has been cancelled.
    //    /// </summary>
    //    protected abstract void OnCancel();


    //    #endregion

    //    #region Draw


    //    /// <summary>
    //    /// Draws the menu.
    //    /// </summary>
    //    public override void Draw(GameTime gameTime)
    //    {
    //        Vector2 position = _position;// new Vector2(100, 150);

    //        // Make the menu slide into place during transitions, using a
    //        // power curve to make things look more interesting (this makes
    //        // the movement slow down as it nears the end).
    //        float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

    //        if (ScreenState == ScreenState.TransitionOn)
    //            position.X -= transitionOffset * 256;
    //        else
    //            position.X += transitionOffset * 512;

    //        // Draw each menu entry in turn.
    //        ScreenManager.SpriteBatch.Begin();

    //        for (int i = 0; i < menuEntries.Count; i++)
    //        {
    //            Color color;
    //            float scale;

    //            if (IsActive && (i == selectedEntry))
    //            {
    //                // The selected entry is yellow, and has an animating size.
    //                double time = gameTime.TotalGameTime.TotalSeconds;

    //                float pulsate = (float)Math.Sin(time * 6) + 1;
                    
    //                color = Color.Yellow;
    //                scale = 1 + pulsate * 0.05f;
    //            }
    //            else
    //            {
    //                // Other entries are white.
    //                color = Color.White;
    //                scale = 1;
    //            }

    //            // Modify the alpha to fade text out during transitions.
    //            color = new Color(color.R, color.G, color.B, TransitionAlpha);

    //            // Draw text, centered on the middle of each line.
    //            Vector2 origin = new Vector2(0, ScreenManager.Font.LineSpacing / 2);

    //            ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, menuEntries[i],
    //                                                 position, color, 0, origin, scale,
    //                                                 SpriteEffects.None, 0);

    //            position.Y += ScreenManager.Font.LineSpacing;
    //        }

    //        ScreenManager.SpriteBatch.End();
    //    }


    //    #endregion
    //}
}
